package com.movitech.mobile.ddzy.modules.common.upload;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.caucho.hessian.client.HessianProxyFactory;
import com.movitech.mobile.cz.base.common.upload.UploadFile;

import hk.modernland.fastdfs.dto.ResultDto;
import hk.modernland.fastdfs.dto.UploadFileDto;
import hk.modernland.fastdfs.service.CommonUploadFileService;

@Component("extUploadFile")
public class ExtUploadFile extends UploadFile {

	@Value("${upload.file.url}")
	private String uploadFileUrl;

	@Value("${upload.file.bizsys}")
	private String uploadFileBizsys;

	@Value("${upload.file.bizmodule}")
	private String uploadFileBizModule;

	@Value("${upload.file.bizrelatedtable}")
	private String uploadFileBizRelatedTable;

	@Value("${upload.file.createor}")
	private String uploadFileCreateor;

	@Value("${upload.file.createorname}")
	private String uploadFileCreateorName;


	@Override
	public String uploadFileAction(MultipartFile multipartFile) {
		String filePath = "";
		try {
			System.out.println("===========UploadFile-->uploadFileAction2");
			String realFileName = multipartFile.getOriginalFilename();//真实上传文件名
			String fileType = "";
			if (realFileName.lastIndexOf(".") >= 0) {
				fileType = realFileName.substring(realFileName.lastIndexOf(".")+1);
			}
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			HessianProxyFactory proxyFactory = new HessianProxyFactory();
			proxyFactory.setOverloadEnabled(true);
			CommonUploadFileService commonUploadFileService = (CommonUploadFileService) proxyFactory.create(CommonUploadFileService.class, uploadFileUrl);
			UploadFileDto uploadFileDto = new UploadFileDto();
			uploadFileDto.setBizSys(uploadFileBizsys);
			uploadFileDto.setBizModule(uploadFileBizModule);
			uploadFileDto.setBizRelatedTable(uploadFileBizRelatedTable);
			uploadFileDto.setBizRelatedGuid(uuid);
			uploadFileDto.setCreateor(uploadFileCreateor);
			uploadFileDto.setCreateorName(uploadFileCreateorName);
			uploadFileDto.setShowFilename(uuid+"."+fileType);
			uploadFileDto.setFileExtName(fileType);
			uploadFileDto.setEncryption(0);
			uploadFileDto.setInputStreamFileBytes(multipartFile.getBytes());
			ResultDto resultDto = commonUploadFileService.uploadFile(uploadFileDto);
			System.out.println("===========UploadFile-->uploadFileAction2==realFileName="+realFileName+"===resultDtoFileName="+resultDto.getFileName()+"===START=");
			filePath = resultDto.getId()+"";
			System.out.println("===========UploadFile-->uploadFileAction2==realFileName="+realFileName+"===resultDtoFileName="+resultDto.getFileName()+"===filePath="+filePath);
			System.out.println(resultDto.getPath());
		} catch (Exception e) {
			System.out.println("===========UploadFile-->uploadFileAction2："+e);
			e.printStackTrace();
		}

		return filePath;
	}

}
